/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 19:45
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using Orz.LogManager.Config;

namespace Orz.LogManager
{
/// <summary>
/// Description of AutoLoginForm.
/// </summary>
public class AutoLoginForm : System.Windows.Forms.Form
{
		private System.Windows.Forms.Button cmdOK;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton optCleartext;
		private System.Windows.Forms.TextBox txtMatch;
		private System.Windows.Forms.RadioButton optEncrypted;
		private System.Windows.Forms.TextBox txtSend;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.TextBox txtSendPassword;
	
	
	private logsetLogfileAutoLogin original_i;	
	private bool bWasCancelled_i;
		
	private HelpForm help_i;
	
	public AutoLoginForm(logsetLogfileAutoLogin autologin_p)
	{
		InitializeComponent();
		
		original_i = autologin_p;
		
		help_i = new HelpForm (this);
		
		PopulateFields();
	}
	
	
	
	public bool WasCancelled
	{
		get { return bWasCancelled_i; }
	}
	
	
	
	
	
	
	private void PopulateFields ()
	{			
		txtMatch.Text = original_i.pattern;				
		
		if (original_i.send.encrypted)
		{
			optCleartext.Checked = false;
			optEncrypted.Checked = true;
			
			txtSend.Text = "";
			
			try
			{
				txtSendPassword.Text = PasswordHelper.Decrypt(original_i.send.Value);
			}
			catch (Exception e)
			{
				MessageBox.Show ("Can't decrypt the stored 'send' text!!", this.Text);
				txtSendPassword.Text = "";
			}
		}
		else
		{
			optCleartext.Checked = true;
			optEncrypted.Checked = false;
			
			txtSend.Text = original_i.send.Value;
			txtSendPassword.Text = "";
		}
				
		UpdateButtons();
	}
	
	private void UpdateButtons()
	{
		txtSend.Enabled = optCleartext.Checked;
		txtSendPassword.Enabled = optEncrypted.Checked;
	}
	
	
	
	
	void OptCleartextCheckedChanged(object sender, System.EventArgs e)
	{
		UpdateButtons();
	}
	
	void OptEncryptedCheckedChanged(object sender, System.EventArgs e)
	{
		UpdateButtons();
	}
	
	
	
	private bool DoSave()
	{
		// Make sure a pattern has been defined
		if (0 == txtMatch.Text.Length)
		{
			MessageBox.Show ("You must specify both a pattern to look for and a string to send in response.", this.Text);
			return false;
		}
		
		if (optCleartext.Checked  &&  0 == txtSend.Text.Length)
		{
			MessageBox.Show ("You must specify both a pattern to look for and a string to send in response.", this.Text);
			return false;
		}
		
		if (optEncrypted.Checked  &&  0 == txtSendPassword.Text.Length)
		{
			MessageBox.Show ("You must specify both a pattern to look for and a string to send in response.", this.Text);
			return false;
		}
		
		
		// Copy the values across to the working version
		original_i.pattern = txtMatch.Text;
		
		original_i.send.encrypted = optEncrypted.Checked;
		
		if (optEncrypted.Checked)
		{
			original_i.send.Value = PasswordHelper.Encrypt(txtSendPassword.Text);
		}
		else
		{
			original_i.send.Value = txtSend.Text;
		}
		
		return true;
	}
	
	
	
	void CmdOKClick(object sender, System.EventArgs e)
	{
		if (DoSave())
		{
			bWasCancelled_i = false;
			this.Close();
		}
	}
	
	void CmdCancelClick(object sender, System.EventArgs e)
	{
		bWasCancelled_i = true;
		this.Close();
	}
	
	
	
	
	
	#region Windows Forms Designer generated code
	/// <summary>
	/// This method is required for Windows Forms designer support.
	/// Do not change the method contents inside the source code editor. The Forms designer might
	/// not be able to load this method if it was changed manually.
	/// </summary>
	private void InitializeComponent() {
			this.txtSendPassword = new System.Windows.Forms.TextBox();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.txtSend = new System.Windows.Forms.TextBox();
			this.optEncrypted = new System.Windows.Forms.RadioButton();
			this.txtMatch = new System.Windows.Forms.TextBox();
			this.optCleartext = new System.Windows.Forms.RadioButton();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.cmdOK = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// txtSendPassword
			// 
			this.txtSendPassword.Location = new System.Drawing.Point(200, 128);
			this.txtSendPassword.Name = "txtSendPassword";
			this.txtSendPassword.PasswordChar = '*';
			this.txtSendPassword.Size = new System.Drawing.Size(160, 20);
			this.txtSendPassword.TabIndex = 1;
			this.txtSendPassword.Tag = "When the text above is sent by the telnet server, Log Manager will send this stri" +
"ng as a response.";
			this.txtSendPassword.Text = "";
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(400, 48);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.TabIndex = 3;
			this.cmdCancel.Tag = "Closes this dialog box, undoing any changes you have made.";
			this.cmdCancel.Text = "&Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCancelClick);
			// 
			// txtSend
			// 
			this.txtSend.Location = new System.Drawing.Point(200, 96);
			this.txtSend.Name = "txtSend";
			this.txtSend.Size = new System.Drawing.Size(160, 20);
			this.txtSend.TabIndex = 1;
			this.txtSend.Tag = "When the text above is sent by the telnet server, Log Manager will send this stri" +
"ng as a response.";
			this.txtSend.Text = "";
			// 
			// optEncrypted
			// 
			this.optEncrypted.Location = new System.Drawing.Point(80, 128);
			this.optEncrypted.Name = "optEncrypted";
			this.optEncrypted.Size = new System.Drawing.Size(112, 24);
			this.optEncrypted.TabIndex = 3;
			this.optEncrypted.Text = "Store encrypted";
			this.optEncrypted.CheckedChanged += new System.EventHandler(this.OptEncryptedCheckedChanged);
			// 
			// txtMatch
			// 
			this.txtMatch.Location = new System.Drawing.Point(200, 32);
			this.txtMatch.Name = "txtMatch";
			this.txtMatch.Size = new System.Drawing.Size(160, 20);
			this.txtMatch.TabIndex = 1;
			this.txtMatch.Tag = "Log Manager will wait until it detects some text that matches this string or regu" +
"lar expression, then it will send the text below in response.";
			this.txtMatch.Text = "";
			// 
			// optCleartext
			// 
			this.optCleartext.Location = new System.Drawing.Point(80, 96);
			this.optCleartext.Name = "optCleartext";
			this.optCleartext.Size = new System.Drawing.Size(120, 24);
			this.optCleartext.TabIndex = 2;
			this.optCleartext.Text = "Store as plain text";
			this.optCleartext.CheckedChanged += new System.EventHandler(this.OptCleartextCheckedChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.optEncrypted);
			this.groupBox1.Controls.Add(this.optCleartext);
			this.groupBox1.Controls.Add(this.txtMatch);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.txtSend);
			this.groupBox1.Controls.Add(this.txtSendPassword);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(376, 168);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(184, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Wait for this text from the server:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(184, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "And then send this string:";
			// 
			// cmdOK
			// 
			this.cmdOK.Location = new System.Drawing.Point(400, 16);
			this.cmdOK.Name = "cmdOK";
			this.cmdOK.TabIndex = 3;
			this.cmdOK.Tag = "Closes this dialog box and temporarily saves the changes you have made.";
			this.cmdOK.Text = "&OK";
			this.cmdOK.Click += new System.EventHandler(this.CmdOKClick);
			// 
			// AutoLoginForm
			// 
			this.AcceptButton = this.cmdOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(488, 184);
			this.Controls.Add(this.cmdOK);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cmdCancel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "AutoLoginForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Auto Login details";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);
		}
	#endregion
	
	
	
}
}
